Loading Libraries
#install.packages('socviz')
#install.packages('usmap')
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.2.1 ✓ purrr 0.3.3
## ✓ tibble 2.1.3 ✓ dplyr 0.8.4
## ✓ tidyr 1.0.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(maps)
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
library(ggthemes)
library(socviz)
library(usmap)
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(scales)
##
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
Reading the data
us_countydata = map_data("county")
us_counties_elections=read.table('county_data.txt', header = T)
us_counties_elections<-us_counties_elections[2:nrow(us_counties_elections),]
str(us_counties_elections$name)
## Factor w/ 1928 levels "1","10","11",..: 1 134 141 152 202 217 278 288 301 349 ...
us_counties_elections$name<- as.character(us_counties_elections$name)
colnames(us_counties_elections)[2]<-"subregion"
#Getting the state name in this dataframe
temp <- data.frame(region=state.name, state= state.abb)
us_counties_elections <- left_join(us_counties_elections, temp, by='state')
## Warning: Column `state` joining factors with different levels, coercing to
## character vector
Join Election data with county data
#Removing missing data
us_counties_elections<-na.omit(us_counties_elections)
#Removing Data for Alaska and Hawaii from election data
us_counties_elections<-us_counties_elections[!(us_counties_elections$region == "Alaska" | us_counties_elections$region == "Hawaii" ), ]
#Changing county names to lowercase
us_counties_elections$subregion<-tolower(us_counties_elections$subregion)
us_countydata$subregion<-tolower(us_countydata$subregion)
us_counties_elections$region<-tolower(us_counties_elections$region)
#Some more data preprocessing
us_counties_elections$subregionn<-gsub('[[:punct:] ]+','',us_counties_elections$subregion)
us_countydata$subregion<-paste(us_countydata$subregion,"county",sep = " ")
us_countydata$subregionn<-gsub('[[:punct:] ]+','',us_countydata$subregion)
counties_mismatch<-as.character(setdiff(us_countydata$subregionn, us_counties_elections$subregionn))
#Finally joining
us_counties_elections_full = left_join(us_countydata, us_counties_elections, by = c("subregionn","region"))
us_counties_elections_full<- na.omit(us_counties_elections_full)
#Some more Data Manipulation
us_counties_elections_full$per_dem_2012<-percent(us_counties_elections_full$per_dem_2012)
us_counties_elections_full$per_dem_2016<-percent(us_counties_elections_full$per_dem_2016)
us_counties_elections_full$per_gop_2012<-percent(us_counties_elections_full$per_gop_2012)
us_counties_elections_full$per_gop_2016<-percent(us_counties_elections_full$per_gop_2016)
us_counties_elections_full$county<-str_remove(us_counties_elections_full$subregionn, "county")
2016 election results
p<-ggplot(us_counties_elections_full, aes(x = long, y = lat, text = paste("County:",county, ",", state , "<br />", "Democrat Vote:", per_dem_2016, "<br />" ,"Republican Vote:",per_gop_2016), group = group, fill = winner))+
geom_polygon(color = "gray90", size = 0.10) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45)+
scale_fill_manual(values = c("dodgerblue3", "indianred3"))+
labs(title = "US PRESIDENTIAL ELECTIONS 2016")+
theme_map()
ggplotly(p, hover='text')
2012 Election Results
library(ggplot2)
p<-ggplot(us_counties_elections_full, aes(x = long, y = lat, text = paste("County:",county, ",", state , "<br />", "Democrat Vote:", per_dem_2012, "<br />", "Republican Vote:", per_gop_2012), group = group, fill = winner12))+
geom_polygon(color = "gray90", size = 0.10) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45)+
scale_fill_manual(values = c("dodgerblue3", "indianred3"))+
labs(title = "US PRESIDENTIAL ELECTIONS 2012")+
theme_map()
ggplotly(p, hover='text')